<template>
  <div class="divBox relative">
    <el-card class="box-card">
      <el-form label-position="right" :model="formData" ref="formSearch" class="mg_t2" onsubmit="return false" :rules="rules" >
        <el-form-item :label="$t('guoJiMianDanHao')" prop="searchCode">
          <scanInput
            v-model.trim="formData.searchCode"
            @enterInput="scanSkuCode"
            style="width: 400px"
            class="form_input_default mg_l1"
            autofocus
          />
          <el-button class="mg_l1" @click="scanSkuCode" type="primary" style="width: 110px">{{ $t('saoMiao') }}</el-button>
          <el-checkbox class="mg_1" v-model="autoPushOver" border @input="configChange">{{ $t('ziDongHuJiaoKuaiDiYuan') }}</el-checkbox>
        </el-form-item>
      </el-form>
    </el-card>
    <div class="mt20"></div>
    <el-card class="box-card">
      <div class="flex_col card_1" key="trackLinkTableDiv" style="height: 260px; box-sizing: border-box;">
        <transition name="el-zoom-in-top" mode="out-in" :duration="200">
          <div v-if="trackSkuTableData.length > 0" class="flex_col flex_cell">
            <el-table :data="trackSkuTableData">
              <el-table-column :label="$t('dingDanHao')" prop="mainOrderCode"/>
              <el-table-column :label="$t('guoJiMianDanHao')" prop="beginExpressNumber"/>
              <el-table-column :label="$t('saoMiaoZhuangTai')" prop="scanStatus">
                <template slot-scope="scope">{{ scope.row.scanStatus === 'Y' ? $t('yiSaoMiao'):$t('weiSaoMiao')}}</template>
              </el-table-column>
            </el-table>
          </div>
          <div v-else class="flex_center flex_cell" style="font-size: 16px;">{{ $t('qingXianSaoMiaoShangPinTiaoMa') }}</div>
        </transition>
        <div class="tool" v-if="trackSkuTableData.length > 0">
          <span>{{ trackSkuMsg }}</span>
          <el-button type="primary" v-show="scanStatus==='Y'&& !autoPushOver" @click="pushCurrentTrackDown">
            {{ $t('huJiaoKuaiDiYuan') }}
          </el-button>
        </div>
      </div>
    </el-card>
    <div class="mt20"></div>
    <el-card class="box-card">
      <el-table :data="tableData">
        <el-table-column :label="$t('dingDanHao')" prop="mainOrderCode"/>
        <el-table-column :label="$t('guoJiMianDanHao')" prop="beginExpressNumberList">
          <template slot-scope="scope">
            <p v-for="(item,index) in scope.row.beginExpressNumberList" :key="index">{{item}}</p>
          </template>
        </el-table-column>
        <el-table-column :label="$t('weiChengFuWuShang')" prop="endExpressNumber"/>
        <el-table-column :label="$t('weiChengPeiSongDanHao')" prop="endExpressCode"/>
        <el-table-column :label="$t('huJiaoKuaiDiYuanFaSongOtp')">
          <template slot-scope="scope">{{ $t('shi') }}</template>
        </el-table-column>
        <el-table-column :label="$t('caoZuo')" width="180" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="saveAs(scope.row.endLabelPath,scope.row.mainOrderCode)">{{ $t('xiaZai') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { scanExpressNumber,changeExpressNumber } from '@/api/order';
import scanInput from '@/components/scanInput'
export default {
  provide() {
    return {
      tabActivatedInject: () => true,
    }
  },
  name: 'changeOrder',
  components:{ scanInput },
  data() {
    return {
      // 自动确认换单
      autoPushOver: false,
      // 是否扫描完成
      scanStatus: 'N',
      formData:{searchCode:''},
      rules: {
        searchCode: {required: true,trigger: 'blur',message:this.$t('qingShuRuGuoJiMianDanHao')},
      },
      // 扫描后待确认订单
      trackSkuTableData: [],
      trackSkuTableOption: {},
      // 已确认订单
      tableData: [],
      tableOption: {},
    };
  },
  mounted() {
    this.autoPushOver = JSON.parse(localStorage.getItem('autoPushOver'));
  },
  methods: {
		//扫描sku
		scanSkuCode() {
			this.$refs.formSearch.validate((valid) => {
				if (valid) {
          scanExpressNumber({ beginExpressNumber: this.formData.searchCode }).then(res=>{
            if (res) {
                this.trackSkuTableData = res.orderDTOList
                this.trackSkuMsg = res.msg
                this.scanStatus = res.scanStatus

                this.tableData = []
                // 自动换单
                if(this.autoPushOver && this.scanStatus==='Y'){
                  this.pushCurrentTrackDown()
                }
							}
          })
				} else {
					return false;
				}
			});
		},
		// 配置自动确认换单
		configChange() {
			localStorage.setItem('autoPushOver', this.autoPushOver);
		},
		// 确认换单
		pushCurrentTrackDown() {
      changeExpressNumber({ orderIds: this.trackSkuTableData.map(it=>it.id) }).then(res=>{
        this.tableData = [res]
      })
    },
    saveAs(url, name) {
      window.open(url, '_blank');
    },
	},
};
</script>
<style lang="scss" scoped>
.flex_center{
  display: flex;
  justify-content: center;
  align-items: center;
}
.flex_cell{
  flex: 1;
}
.flex_col {
  display: flex;
  flex-direction: column;
}

.tool{
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 10px;
}
</style>
